package LeetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LC_500_KeyboardRow {

    public static void main(String[] args) {

    }

    public class Solution {
        public String[] findWords(String[] words) {
            List<String> results = new ArrayList<>();
            char[] row1 = new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
            char[] row2 = new char[]{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
            char[] row3 = new char[]{'z', 'x', 'c', 'v', 'b', 'n', 'm'};
            Map<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < 10; i++)
                map.put(row1[i], 1);
            for (int i = 0; i < 9; i++)
                map.put(row2[i], 2);
            for (int i = 0; i < 7; i++)
                map.put(row3[i], 3);
            int row = 0;
            for (String word1 : words) {
                String word = word1.toLowerCase();
                int len = word.length(), j = 0, flag = 0;
                for (; j < len; j++) {
                    if (flag == 0) {
                        row = map.get(word.charAt(j));
                        flag = 1;
                    } else {
                        if (map.get(word.charAt(j)) != row) {
                            break;
                        }
                    }
                }
                if (j == len)
                    results.add(word1);
            }
            int size = results.size();
            String[] result = new String[size];
            for (int i = 0; i < size; i++)
                result[i] = results.get(i);
            return result;
        }

    }
}